/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.ServiceConfig;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author GiangLT
 */
@Stateless
public class ServiceConfigFacade extends AbstractFacade<ServiceConfig> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ServiceConfigFacade() {
        super(ServiceConfig.class);
    }

    public List<ServiceConfig> findByServiceName(String serviceName, Date startEffect, Date endEffect) {
        try {
            TypedQuery<ServiceConfig> q = em.createNamedQuery("ServiceConfig.findByServiceName", ServiceConfig.class);
            q.setParameter("serviceName", serviceName);
            q.setParameter("startEffect", startEffect);
            q.setParameter("endEffect", endEffect);
            List<ServiceConfig> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(ServiceConfigFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<ServiceConfig> findGeneralServices(String... serviceNames) {
        try {
            TypedQuery<ServiceConfig> q = em.createNamedQuery("ServiceConfig.findGeneralServices", ServiceConfig.class);
            StringBuilder sb = new StringBuilder("'");
            for (String serviceName : serviceNames) {
                sb.append(serviceName);
                sb.append("', '");
            }
            sb.delete(sb.length() - 2, sb.length());
            q.setParameter("serviceNames", sb.toString());
            List<ServiceConfig> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(ServiceConfigFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public void tinhKet(String maTinh, Integer doDaiCau, int soNgayAn, int soNgayThongKe, Date startDate, Date endDate) throws Exception {
        try {
            Query q = em.createNativeQuery("{call TINH_KET(?,?,?,?,?,?)}");

            q.setParameter(1, maTinh);
            q.setParameter(2, doDaiCau);
            q.setParameter(3, soNgayAn);
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

            q.setParameter(4, dateFormat.format(startDate));
            q.setParameter(5, dateFormat.format(endDate));
            q.setParameter(6, soNgayThongKe);
            q.executeUpdate();
        } catch (Exception ex) {
            LoggerFactory.getLogger(ServiceConfigFacade.class.getName()).error("", ex);
            throw ex;
        }
    }

    public void tinhCau(String maTinh, Integer doDaiCau, int soNgayAn, int soNgayThongKe, Date startDate, Date endDate) throws Exception {
        try {
            Query q = em.createNativeQuery("{call TINH_CAU_TEMP(?,?,?,?,?,?)}");

            q.setParameter(1, maTinh);
            q.setParameter(2, doDaiCau);
            q.setParameter(3, soNgayAn);
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

            q.setParameter(4, dateFormat.format(startDate));
            q.setParameter(5, dateFormat.format(endDate));
            q.setParameter(6, soNgayThongKe);
            q.executeUpdate();
        } catch (Exception ex) {
            LoggerFactory.getLogger(ServiceConfigFacade.class.getName()).error("", ex);
            throw ex;
        }
    }

    public void rollbackCau(int soNgay) throws Exception {
        try {
            Query q = em.createNativeQuery("{call rollback_cau(?)}");
            q.setParameter(1, soNgay);
            q.executeUpdate();
        } catch (Exception ex) {
            LoggerFactory.getLogger(ServiceConfigFacade.class.getName()).error("", ex);
            throw ex;
        }
    }
}
